home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Utilities / Programming / Snippets 2.4 / Snippets / Snippets.rsrc / TEXT_405_5.txt < prev    next >
Encoding:
Text File  |  1996-01-21  |  10.5 KB  |  397 lines

  1. /**********************************************************************
  2.  
  3.     SampleList.cp
  4.  
  5. ***********************************************************************/
  6.  
  7. /*
  8.     A sample list for the Starter application.
  9. */
  10.  
  11. /********** Includes */
  12. #include <Dialogs.h>
  13. #include "Fn_Prototypes.h"
  14. #include "MainWindow.h"
  15.  
  16. /********** Defines */
  17. #define LIST_DLOG      510
  18. #define NIL_PTR        0L
  19. #define ALLOCATE_MEM   0
  20. #define IN_FRONT       (WindowPtr)-1L
  21. #define OK_BUTTON      1
  22. #define LIST_ITEM      2
  23. #define RETURN_KEY     13
  24. #define ENTER_KEY      3
  25. #define VISUAL_DELAY   8  // standard is 8 ticks
  26.  
  27. ListHandle theList;
  28.  
  29. /********** Prototypes */
  30. Boolean        MyListDialog( void );
  31. pascal Boolean MyListEventFilter(  DialogPtr     theDialog,
  32.                                    EventRecord   *theEvent,
  33.                                    short         *itemHit );
  34. void           MyListDoContent(    WindowPtr     w, 
  35.                                    EventRecord   *e,
  36.                                    short         *itemHit );
  37. void           MyFrameList(        WindowPtr     w,
  38.                                    ListHandle    *myList );
  39.  
  40. /********** MyListDialog */
  41.  
  42. Boolean MyListDialog( void )
  43. {
  44.     WindowPtr     docWindow;
  45.     DialogPtr     dialog;
  46.     Boolean       done;
  47.     Boolean       result;
  48.     short         itemHit;
  49.     short         itemType;
  50.     Handle        itemHandle;
  51.     Rect          itemRect;
  52.     EventRecord   theEvent;
  53.     long          finalTicks;
  54.     
  55.     ListHandle    myList;
  56.     Rect          myDataBounds;
  57.     Point         myCellSize;
  58.     Rect          myRect;   
  59.     const Boolean kDoDraw = TRUE;
  60.     const Boolean kNoGrow = FALSE;
  61.     const         kIncludeScrollBar = TRUE;
  62.     const         kScrollBarWidth = 15;
  63.     const         kColumnsInList = 1;
  64.     const         kTextLDEF = 0;
  65.     int           myRowNum;
  66.     int           myIndex;
  67.     Cell          myCell;
  68.     int           i;
  69.     char          *c;
  70.     
  71.     Str255        myStrArray[21] = {
  72.       "\pZero",
  73.       "\pOne",
  74.       "\pTwo",
  75.       "\pThree",
  76.       "\pFour",
  77.       "\pFive",
  78.       "\pSix",
  79.       "\pSeven",
  80.       "\pEight",
  81.       "\pNine",
  82.       "\pTen",
  83.       "\pEleven",
  84.       "\pTwelve",
  85.       "\pThirteen",
  86.       "\pFourteen",
  87.       "\pFifteen",
  88.       "\pSixteen",
  89.       "\pSeventeen",
  90.       "\pEighteen",
  91.       "\pNineteen",
  92.       "\pTwenty" };
  93.                                       
  94.     
  95.     result = FALSE;
  96.  
  97.     docWindow = FrontWindow();
  98.     if( docWindow != NIL_PTR )
  99.         MyDoDeactivateWindow( docWindow );
  100.  
  101.     dialog = GetNewDialog( LIST_DLOG, ALLOCATE_MEM, IN_FRONT );
  102.  
  103.     if( dialog == NIL_PTR )
  104.         return( result );
  105.  
  106.     /* AdjustMenus_(); */
  107.     
  108.     ShowWindow( dialog );
  109.     FnMisc_FrameButton( dialog, OK_BUTTON );
  110.  
  111.     GetDItem( dialog, LIST_ITEM, &itemType, &itemHandle, &itemRect );
  112.     myRect = itemRect;
  113.     
  114.     /* set-up list */
  115.     
  116.     /* start with a list with one column and no rows */
  117.     SetRect( &myDataBounds, 0, 0, kColumnsInList, 0 );
  118.     
  119.     /* let list manager calculate size of a cell */
  120.     SetPt( &myCellSize, 0, 0 );
  121.     
  122.     /* adjust the rectangle to leave room for scroll bar */
  123.     myRect.right = myRect.right - kScrollBarWidth;
  124.     
  125.     /* create the list */
  126.     myList = LNew( &myRect, &myDataBounds, myCellSize,
  127.         kTextLDEF, (WindowPtr)dialog, kDoDraw, kNoGrow,
  128.         !kIncludeScrollBar, kIncludeScrollBar );
  129.     (**myList).selFlags = lOnlyOne;
  130.     theList = myList;
  131.  
  132.     /* frame list */
  133.     MyFrameList( (WindowPtr)dialog, &myList );
  134.     
  135.     /* add strings to list */
  136.     LDoDraw( FALSE, myList );
  137.     myRowNum = (**myList).dataBounds.bottom;
  138.     myIndex = 1;
  139.     for( i = 1; i <= 20; i++ )
  140.     {
  141.         myRowNum = LAddRow( 1, myRowNum, myList );
  142.         SetPt( &myCell, 0, myRowNum );
  143.         c = (char *)myStrArray[myIndex];
  144.         c = c + 1;
  145.         LSetCell( c, StrLength(myStrArray[myIndex]), myCell, myList );
  146.         myRowNum = myRowNum + 1;
  147.         myIndex = myIndex + 1;
  148.     }
  149.     LDoDraw( TRUE, myList );
  150.     LUpdate( dialog->visRgn, myList );
  151.  
  152.     done = FALSE;
  153.     while( done == FALSE )
  154.     {
  155.         ModalDialog( &MyListEventFilter, &itemHit );
  156.  
  157.         switch( itemHit )
  158.         {
  159.             case OK_BUTTON:
  160.                 result = TRUE;
  161.                 done = TRUE;
  162.                 break;
  163.             case LIST_ITEM:
  164.                 result = TRUE;
  165.                 break;
  166.             default:
  167.                 break;
  168.         }
  169.      }
  170.  
  171.     DisposDialog( dialog );
  172.  
  173.     return( result );
  174. }
  175.  
  176.  
  177. /********** PASCAL MyListEventFilter */
  178.  
  179. pascal Boolean MyListEventFilter( DialogPtr    theDialog,
  180.                                   EventRecord  *theEvent,
  181.                                   short        *itemHit )
  182. {
  183.     short          thePart;
  184.     char           key;
  185.     short          itemType;
  186.     Handle         itemHandle;
  187.     Rect           itemRect;
  188.     long           finalTicks;
  189.     Rect           dragRect;
  190.     Boolean        result;
  191.     Point          theLocation;
  192.     WindowPtr      theWindow;
  193.     
  194.     result = FALSE;
  195.     dragRect = screenBits.bounds;
  196.     
  197.         switch( (*theEvent).what )
  198.         {
  199.             case mouseDown:
  200.                 thePart = FindWindow( (*theEvent).where, &theWindow );
  201.                 if( theWindow == theDialog )
  202.                 {
  203.                     switch( thePart )
  204.                     {
  205.                         case inDrag:
  206.                             DragWindow( theDialog,
  207.                                         (*theEvent).where,
  208.                                         &dragRect );
  209.                             result = TRUE;
  210.                             break;
  211.                         case inContent:
  212.                             if( theDialog == FrontWindow() )
  213.                             {
  214.                                 MyListDoContent( (WindowPtr)theDialog,
  215.                                     theEvent, itemHit );
  216.                                 result = TRUE;
  217.                             }
  218.                             break;
  219.                         default:
  220.                             break;
  221.                      }
  222.                  }
  223.                 break;
  224.             case keyDown:
  225.             case autoKey:
  226.                 key = (*theEvent).message & charCodeMask;
  227.                 if( (key == RETURN_KEY) || (key == ENTER_KEY) )
  228.                 {
  229.                     GetDItem( theDialog,
  230.                               OK_BUTTON,
  231.                               &itemType,
  232.                               &itemHandle,
  233.                               &itemRect );
  234.                     HiliteControl( (ControlHandle)itemHandle,
  235.                                    inButton );
  236.                     Delay( VISUAL_DELAY, &finalTicks );
  237.                     HiliteControl( (ControlHandle)itemHandle, 0 );
  238.                     *itemHit = OK_BUTTON;
  239.                     result = TRUE;
  240.                 }
  241.                 /* Handle other keyboard equivalents here */
  242.                 break;
  243.             case updateEvt:
  244.                 if( (WindowPtr)(*theEvent).message != theDialog )
  245.                 {
  246.                     MyDoUpdateWindow( (WindowPtr)(*theEvent).message );
  247.                 }
  248.                 else
  249.                 {
  250.                     MyFrameList( (WindowPtr)theDialog, &theList );
  251.                     LUpdate( theDialog->visRgn, theList );
  252.                     FnMisc_FrameButton( theDialog, OK_BUTTON );
  253.                 }
  254.                 break;
  255.             case activateEvt:
  256.                 if( (WindowPtr)(*theEvent).message != theDialog )
  257.                 {
  258.                     /*
  259.                     DoActivate_( (WindowPtr)(*theEvent).message,
  260.                                  ((*theEvent).modifiers & activeFlag),
  261.                                  *theEvent );
  262.                     */
  263.                  }
  264.                  break;
  265.             default:
  266.                  break;
  267.         }
  268.         
  269.     return( result );
  270. }
  271.  
  272.  
  273. /********** MyListDoContent */
  274.  
  275. void MyListDoContent( WindowPtr     w, 
  276.                       EventRecord   *e,
  277.                       short         *itemHit )
  278. /*
  279.     Use this procedure when user clicks in content region of TE window.
  280.     Uses the TnTE_ScrollProc below to take care of scroll bar function.
  281.     Also uses FnTE_AdjustText function.
  282. */
  283. {
  284.     int                cntlCode;
  285.     ControlHandle      cntl;
  286.     GrafPtr            savePort;
  287.  
  288.     short         itemType;
  289.     Handle        itemHandle;
  290.     Rect          itemRect;
  291.     long          finalTicks;
  292.     
  293.     GetPort(&savePort);
  294.     SetPort(w);
  295.     *itemHit = 0;
  296.     GlobalToLocal(&e->where);
  297.     if( (cntlCode = FindControl(e->where, w, &cntl)) == inButton )
  298.     {
  299.         if( TrackControl( cntl, e->where, 0L ) )
  300.             *itemHit = OK_BUTTON;
  301.     }
  302.     else
  303.     {
  304.         GetDItem( (DialogPtr)w, LIST_ITEM, &itemType, &itemHandle,
  305.             &itemRect );
  306.         if( PtInRect( e->where, &itemRect ) )
  307.         {
  308.             *itemHit = LIST_ITEM;
  309.             if( LClick( e->where, e->modifiers, theList ) )
  310.             {
  311.             
  312.                 GetDItem( (DialogPtr)w,
  313.                           OK_BUTTON,
  314.                           &itemType,
  315.                           &itemHandle,
  316.                           &itemRect );
  317.                 HiliteControl( (ControlHandle)itemHandle,
  318.                                inButton );
  319.                 Delay( VISUAL_DELAY, &finalTicks );
  320.                 HiliteControl( (ControlHandle)itemHandle, 0 );
  321.                 *itemHit = OK_BUTTON;
  322.             }
  323.         }
  324.     }
  325.     
  326.     SetPort(savePort);
  327. }
  328.  
  329.  
  330. /********** MyFrameList */
  331.  
  332. void MyFrameList( WindowPtr w, ListHandle *myList )
  333. {
  334.     Rect     myBorder;
  335.     PenState myPenState;
  336.     GrafPtr  savePort;
  337.     
  338.     savePort = thePort;
  339.     SetPort( w );
  340.     myBorder = (***myList).rView;
  341.     GetPenState( &myPenState );
  342.     PenNormal();
  343.     PenSize(1,1);
  344.     InsetRect( &myBorder, -1, -1 );
  345.     FrameRect( &myBorder );
  346.     SetPenState( &myPenState );
  347.     SetPort( savePort );
  348. }
  349.  
  350.  
  351. /**********************************************************************
  352.  
  353.     SampleList.r
  354.  
  355. ***********************************************************************/
  356.  
  357. /* THINK Rez resource format */
  358. #include <Types.r>
  359.  
  360. resource 'DLOG' (510, "List Dialog") {
  361.     {45, 10, 317, 205},
  362.     movableDBoxProc, /* movableDBoxProc or noGrowDocProc */
  363.     invisible,
  364.     goAway,
  365.     0x0,
  366.     510,
  367.     "List Dialog"
  368. };
  369.  
  370. resource 'DITL' (510, "List Dialog") {
  371.     {    /* array DITLarray: 1 elements */
  372.         /* [1] */
  373.         {242, 68, 262, 128},
  374.         Button {
  375.             enabled,
  376.             "OK"
  377.         },
  378.         /* [2] */
  379.         {5, 5, 229, 190},
  380.         UserItem {
  381.             disabled
  382.         }
  383.     }
  384. };
  385.  
  386.  
  387. /**********************************************************************
  388.  
  389.     SampleList.h
  390.  
  391. ***********************************************************************/
  392.  
  393. /********** Prototypes */
  394. extern Boolean MyListDialog( void );
  395.  
  396. // End of File
  397.